package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.dao.OrderdetailetDao;
import com.foreknow.elm.dao.OrdersDao;
import com.foreknow.elm.dao.impl.CartDaoImpl;
import com.foreknow.elm.dao.impl.OrderdetailetDaoImpl;
import com.foreknow.elm.dao.impl.OrdersDaoImpl;

import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.OrderDetaiLet;
import com.foreknow.elm.po.Orders;
import com.foreknow.elm.service.OrdersService;
import com.foreknow.elm.util.DBUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


/**
 * 根据用户编号、商家编号、订单总金额、送货地址编号向订单表中添加一条记录，
 * ​           并获取自动生成的订单编号。
 * <p>
 * ​           然后根据用户编号、商家编号从购物车表中查询所有数据，批量添加到订单明细表中。
 * <p>
 * ​           然后根据用户编号、商家编号删除购物车表中的数据。
 * 开启事务
 * 1.1查询购物车当中所有的食品信息（目的要将查询的信息放在订单明细中）
 * 1.2创建一个订单，并返回订单编号
 * 1.3向订单明细表中批量添加数据  需要创建orderdetailetDao和实现类
 * 1.4清空购物车
 * 提交事务
 * 回滚事务
 */
public class OrdersServiceImpl implements OrdersService {
    @Override
    public Integer createOrders(String userId, Integer businessId, Integer daId, Double orderTotal) {
        int orId = 0;
        List<OrderDetaiLet> letList = new ArrayList<>();
        CartDao cartDao = new CartDaoImpl();
        OrdersDao ordersDao = new OrdersDaoImpl();
        OrderdetailetDao orderdetailetDao = new OrderdetailetDaoImpl();
        Cart cart = new Cart();
        cart.setUserId(userId);
        cart.setBusinessId(businessId);
        try {
            DBUtil.beginTransaction();
            List<Cart> list = cartDao.selectCart(cart);
            orId = ordersDao.selectByOrdersId(userId, businessId, daId, orderTotal);
            OrderDetaiLet orderDetaiLet = null;
            for (Cart c : list) {
                orderDetaiLet = new OrderDetaiLet();

                orderDetaiLet.setOrderId(orId);
                orderDetaiLet.setFoodId(c.getFoodId());
                orderDetaiLet.setQuantity(c.getQuantity());
                letList.add(orderDetaiLet);
            }
            orderdetailetDao.saveOrdersdetailrt(letList);
            cartDao.removeCartOnly(cart);
            DBUtil.commitTransaction();
        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return orId;
    }

    @Override
    public Orders getOrdersById(Integer orderId) {
        Orders orders = new Orders();
        OrdersDao ordersDao = new OrdersDaoImpl();
        OrderdetailetDao orderdetailetDao=new OrderdetailetDaoImpl();
        try {
            orders = ordersDao.getOrdersById(orderId);
            List<OrderDetaiLet> letList = orderdetailetDao.selectOrderDetaiLet(orderId);
            orders.setOrderDetaiLetList(letList);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return orders;
    }

    @Override
    public List<Orders> listOrdersByUserId(String userId) {
        List<Orders> list = new ArrayList<>();
        OrdersDao ordersDao = new OrdersDaoImpl();
        OrderdetailetDao orderdetailetDao = new OrderdetailetDaoImpl();
        try {
            list = ordersDao.listOrdersByUserId(userId);
            for (Orders o : list) {
                List<OrderDetaiLet> letList = orderdetailetDao.selectOrderDetaiLet(o.getOrderId());
                o.setOrderDetaiLetList(letList);

            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return list;
    }

    @Override
    public Integer updateState(Integer orderId) {
        int result=0;
        OrdersDao ordersDao=new OrdersDaoImpl();
        try {
            result=ordersDao.updateState(orderId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }


}
